<template>
  <div>
    <el-row>
      <el-col :span="20">
        <div class="grid-content bg-purple-dark">客户操作页面</div>
      </el-col>
    </el-row>
    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item label="审批人">
        <el-input  placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="客户类型">
        <el-select  placeholder="客户类型">
          <el-option label="供应商" value="shanghai"></el-option>
          <el-option label="销售商" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-col :span="20">
        <template>
          <el-table :data="tableData" height="250" border style="width: 100%">
            <el-table-column prop="date" label="日期" width="180"></el-table-column>
            <el-table-column prop="name" label="姓名" width="180"></el-table-column>
            <el-table-column prop="custype" label="客户类型"></el-table-column>
            <el-table-column prop="cusemail" label="客户邮箱"></el-table-column>
            <el-table-column prop="address" label="地址"></el-table-column>
            <el-table-column fixed="right" label="操作" width="130">
              <div slot-scope="scope">
                <!-- <template slot-scope="scope"> -->
                <el-button @click="handledele(scope)" type="text" size="small">删除</el-button>
                <el-button @click="handleClick(scope)" type="text" size="small">编辑</el-button>
                <el-button @click="handadd" type="text" size="small">添加</el-button>

                <!-- </template> -->
              </div>
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
    <!-- 添加客户信息 -->
    <el-dialog style="z-index:999" title="添加客户信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" status-icon :rules="rules" ref="form">
        <el-form-item label="客户姓名"  prop="cusName" :label-width="formLabelWidth">
          <el-input class="cusnameClass" v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="客户类型" prop="cusType" :label-width="formLabelWidth">
          <el-select v-model="form.custype" placeholder="请选择客户类型">
            <el-option label="供应商" prop="cusEmail"  value="供应商"></el-option>
            <el-option label="销售商" value="销售商"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户邮箱" prop="cusEmail" :label-width="formLabelWidth">
          <el-input v-model="form.cusemail" class="cusemailClass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" prop="cusAddress" :label-width="formLabelWidth">
          <el-input
            class="cusaddressClass"
            v-model="form.address"
            autocomplete="off"
            style="width:50%"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑用户信息 -->
    <el-dialog style="z-index:999" title="编辑客户信息" :visible.sync="dialogFormVisibleEdit">
      <el-form :model="form">
        <el-form-item label="客户姓名" prop="cusName" :label-width="formLabelWidth">
          <el-input class="cusnameClass" v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="客户类型" prop="cusType" :label-width="formLabelWidth">
          <el-select v-model="form.custype" placeholder="请选择客户类型">
            <el-option label="供应商" value="供应商"></el-option>
            <el-option label="销售商" value="销售商"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户邮箱" prop="cusEmail" :label-width="formLabelWidth">
          <el-input v-model="form.cusemail" class="cusemailClass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" prop="cusAddress" :label-width="formLabelWidth">
          <el-input
            class="cusaddressClass"
            v-model="form.address"
            autocomplete="off"
            style="width:50%"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editCusinfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    var checkname = (rule, value, callback) => {
      if (value==="") {
        return callback(new Error("姓名不可为空"));
      }
      callback();
    };

    var validateEmail = (rule, value, callback) => {
      let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      
      if (value === "") {
        callback(new Error("请输入邮箱号码"));
      } else if(value!==""){
        if(!value.match(reg)){
           this.$refs.form.validateField("cusEmail");
              callback(new Error("请填写正确的格式"));
          }
      }
      callback();
    };

    return {

      rules: {
        cusName: [{ validator: checkname, trigger: "blur" }],
        cusEmail: [{ validator: validateEmail, trigger: "blur" }]
      },
      tableData: [
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "供应商",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "供应商",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "供应商",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "联系人",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-08",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "供应商",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-06",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "供应商",
          cusemail: "2535290272@qq.com"
        },
        {
          date: "2016-05-07",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          custype: "联系人",
          cusemail: "2535290272@qq.com"
        }
      ],

      dialogFormVisible: false,
      dialogFormVisibleEdit: false,
      form: {
        name: "",
        custype: "",
        cusemail: "",
        address: "",
        date: this.getDate()
      },
      formLabelWidth: "120px",
      _index: 0,
      _row: {
        name: "",
        custype: "",
        cusemail: "",
        address: "",
        date: ""
      }
    };
  },
  methods: {
    onSubmit() {
      console.log("submit!");
    },
    handleClick(scope) {
      this.dialogFormVisibleEdit = true;
      console.info(scope);
      this._index = scope.$index;
      this._row = scope.row;

      for (var index in this.form) {
        this.form[index] = this._row[index];
      }
    },
    handledele(scope) {
      console.info(scope);
      this.tableData.splice(scope.$index, 1);
      // console.log(scope.$index)
    },
    open(row) {
      this.$alert("<template>132456</template>", "添加用户", {
        confirmButtonText: "确定"
        // callback: action => {
        //   this.$message({
        //     type: "info",
        //     message: `action: ${action}`
        //   });
        // }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.addcus();
          this.dialogFormVisible = false;
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName){
      this.dialogFormVisible=false
      this.$refs[formName].resetFields();
    },
    addcus() {
      let Nfrom = {
        name: "",
        custype: "",
        cusemail: "",
        address: "",
        date: this.getDate()
      };

      for (var item in this.form) {
        for (var index in Nfrom) {
          if (item == index && index != "date") {
            Nfrom[index] = this.form[item];
          }
        }
      }
      this.tableData.push(Nfrom);
    },
    getDate() {
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let days = date.getDate();
      let currdate = year + "-" + month + "-" + days;
      return currdate;
    },
    handadd() {
      this.dialogFormVisible = true;
      for (var index in this.form) {
        this.form[index] = "";
      }
    },
    editCusinfo() {
      let Nfrom = {
        name: "",
        custype: "",
        cusemail: "",
        address: "",
        date: this.getDate()
      };

      for (var item in this.form) {
        for (var index in Nfrom) {
          if (item == index && index != "date") {
            Nfrom[index] = this.form[item];
          }
        }
      }
      this.tableData.splice(this._index, 1, Nfrom);

      this.dialogFormVisibleEdit = false;
    }
  }
};
</script>

<style>
.el-row {
  margin-bottom: 20px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  text-align: center;
  line-height: 36px;
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.cusnameClass,
.cusemailClass {
  width: 40% !important;
}
</style>